/*
卷积滤波器实现
管理卷积核的权重初始化和更新
支持多种滤波器类型和训练策略
*/
#include "Filter.h"
#include <iostream>
#include <stdexcept>

Filter::Filter(int filterIndex, int inputChannels, int filterWidth, int filterHeight) :
    filterIndex(filterIndex), inputChannels(inputChannels), filterWidth(filterWidth), filterHeight(filterHeight) {
    // Validate parameters
    if (filterWidth <= 0 || filterHeight <= 0 || inputChannels <= 0) {
        throw std::invalid_argument("Filter dimensions must be positive.");
    }

    // Initialize weights and biases with random values
    weights = Tensor<double, 3>(filterWidth, filterHeight, inputChannels);
    biases = Tensor<double, 1>(0);
    #ifdef USE_USER_DEFINED_TENSOR 
        if (weights.getSize() == 0 || biases.getSize() == 0) {
            throw runtime_error("Memory allocation failed for weights or biases.");
        }
    #endif
    #ifdef USE_EIGEN_TENSOR 
        if (weights.size() == 0 || biases.size() == 0) {
            throw runtime_error("Memory allocation failed for weights or biases.");
        }
    #endif
    weights.setRandom();
    biases.setRandom();
}
Filter::~Filter() {}
const int Filter::getFilterIndex() const {
    return filterIndex;
}
const int Filter::getInputChannels() const {
    return inputChannels;
}
const int Filter::getFilterWidth() const {
    return filterWidth;
}
const int Filter::getFilterHeight() const {
    return filterHeight;
}
const Tensor<double, 3> Filter::getWeights() const {
    return weights;
}
const Tensor<double, 1> Filter::getBiases() const {
    return biases;
}
void Filter::setWeights(const Tensor<double, 3>& weights) {
    this->weights = weights;
}
void Filter::setBiases(const Tensor<double, 1>& biases) {
    this->biases = biases;
}
void Filter::print() const {
    cout << "Filter " << filterIndex << ":" << endl;
    cout << "Weights:" << endl;
    weights.print();
    cout << "Biases:" << endl;
    biases.print();
}

